# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: 4hu图片.py
@time: 2021/8/23 16:15
'''
from icecream import ic
import asyncio
import os
import time
from lxml import etree
import requests
from pyppeteer import launch
from pyquery import PyQuery
from concurrent.futures import ThreadPoolExecutor

''':cvar
https://www.4hu.com/pic/meitui/index_2.html

https://www.4hu.com/view/202108/26806.html

'''

js = '''
    var monitorObj = {
     imgTotal: 0,
     imgLoaded: 0
 }
 //创建a标签，赋予图片对象相关属性，并插入body
 var createA = function (obj) {
     var a = document.createElement("a");
     a.id = obj.id;
     a.target = "_blank";//注意：要在新页面打开
     a.href = obj.url;
     a.download = obj.url;
 
     document.body.appendChild(a);
 }
 
 //获取页面的图片
 var imgs = document.images;
 //创建每个图片对象的对应a标签
 for (var i = 0; i < imgs.length;i++){
     var obj = {
         id: "img_" + i,
         url: imgs[i].src
     }
     //过滤掉不属于这几种类型的图片
     if (["JPG", "JPEG", "PNG","GIF"].indexOf(obj.url.substr(obj.url.lastIndexOf(".")+1).toUpperCase()) < 0) {
         continue;
     }
     //这里是为了去掉知乎用户头像的图片，头像大小是50*50
     if (imgs[i].width <= 50 || imgs[i].height <= 50) {
         continue;
     }
     //统计图片数量
     monitorObj.imgTotal++;
     createA(obj);
 }
 //开始下载图片
 for (var i = 0; i < imgs.length; i++) {
     if (document.getElementById("img_" + i)) {
         //重点：触发a标签的click事件
         document.getElementById("img_" + i).click();
         monitorObj.imgLoaded++; //统计已下载的图片数量
     }
 }
 console.log("已下载："+monitorObj.imgLoaded + "/" + monitorObj.imgTotal);

'''

async def main(url): # 启动一个浏览器
    browser = await launch(headless=True,args=['--disable-infobars','--window- size=1920,1080']) # 创建一个页面
    page = await browser.newPage()
    await page.setViewport({'width': 1200, 'height': 800})
    # 伪装
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,''{ webdriver:{ get: () => false } })}')
    asyncio.wait(3)
    await page.setUserAgent(
        "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5")

    await page.goto(url,{'timeout': 1000*120 })

    time.sleep(3)
    #  从触发JS
    dimensions = await page.evaluate('''() => { return { cookie: window.document.cookie, } }''')
    print(dimensions, type(dimensions))
    html = await page.content()
    data = etree.HTML(html)
    image_url = data.xpath('//div[@class="mod channel-list"]/div[@class="row col6 clearfix"]/dl/dt/a/@href')
    bas_url = 'https://www.4hu.com'
    try:
        for u in image_url:
            image_full_url = bas_url + u
            await page.goto(image_full_url,{'timeout': 1000*1200})
            await page.evaluate(js)
            ic(u + '>>>>>>>>done')
            # data1 = await page.content()
            # html1 = etree.HTML(data1)
            # image = html1.xpath('//div[@class="pic"]/img/@src')
            # for i in image:
            #     await page.goto(i)

            # content = page.content()
            # print(content,type(content))
            # path = 'g:/'+'4huimage/'
            # if os.path.exists(path) is False:
            #     os.mkdir(path)
            # name = i.split('/')[-1]
            # with open(path+name,'wb') as f1:
            #     f1.write(content)
    except Exception:
        print('出错了')


async def run():
    pass
url = 'https://www.4hu.com/pic/meitui/index_2.html'

asyncio.get_event_loop().run_until_complete(main(url))


